<extend name="Public/base"/>
<block name="style" >
    <link rel="stylesheet" href="__STATIC__/styles/vendors/bootstrap-table.css">
    <!-- <link rel="stylesheet" href="__STATIC__/styles/contact_manage.css"> -->
    <link rel="stylesheet" href="__STATIC__/styles/user_manage.css">
    <link rel="stylesheet" href="__STATIC__/styles/vendors/bootstrap-select.min.css">
    <link rel="stylesheet" href="__STATIC__/plugins/dropper/jquery.fs.dropper.min.css">
    <style type="text/css">
        li.list-group-item.node-tree {
        display: flex;
        }
    </style>
</block>
<block name="content">

    <div id="tree-container">
        <div id="tree"></div>
    </div>

    <div id="table-container">
        <div id="toolbar">
            <button id="remove" class="btn btn-danger btn-flat" disabled>
                <i class="glyphicon glyphicon-remove"></i>删除
            </button>
            <button id="import" class="btn btn-primary btn-flat">导入通讯录</button>
            <!--<button id="add" class="btn btn-primary btn-flat">-->
                <!--添加联系人-->
            <!--</button>-->
            <!--<button id="group-manage" class="btn btn-primary btn-flat">-->
                <!--群组管理-->
            <!--</button>-->
        </div>

        <table id="table" data-toolbar="#toolbar" data-search="true"></table>
    </div>

    <div class="modal fade" id="import_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="container-fluid">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title">通讯录
                            导入</h4>
                    </div>
                    <form id="importForm" enctype="multipart/form-data" action="{:U('import')}" method="post">
                        <div class="modal-body">

                            <div class="control-group row">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                                    <label class="control-label" >文件:</label>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                                    <div class="controls">
                                        <input type="file" id="file_import" class="form-control" name="file_import">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" id="modelLoad" class="btn btn-primary btn-flat pull-left">模版下载</button>
                            <button type="submit" id="importButton" class="btn btn-danger">导入</button>
                            <button type="button" class="btn btn-default btn-flat" data-dismiss="modal">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>


    <div class="modal fade" id="group_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">群组管理</h4>
                </div>
                <div class="modal-body">
                    <table id="group-table">
                    </table>
                    <div class="form-group" id="table2-tools">
                        <select id="delOption" class="selectpicker">
                            <option value="0">只删组</option>
                            <option value="1">连同联系人</option>
                        </select>
                        <button id="deleteGroup" class="btn btn-danger btn-flat" disabled>删除</button>
                        <button class="btn btn-primary btn-flat" id="add-group">添加</button>
                    </div>
                    <div id="table2-alert"></div>

                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default btn-flat" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="complex_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2">
        <div class="modal-dialog " role="document">
            <form id="editContact" class="modal-content" method="post" action="{:U('editContact')}">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">添加联系人</h4>
                </div>

                <div class="modal-body">

                    <div class="control-group row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                            <label class="control-label" for="real_name">姓名:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <input type="text" class="form-control" id="real_name" name="real_name">
                            </div>
                        </div>
                    </div>

                    <div class="control-group row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                            <label class="control-label" for="sex">性别:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <select name="sex" id="sex" class="form-control">
                                    <option value="0" selected="selected">男</option>
                                    <option value="1">女</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="control-group row addPhoneDiv">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                            <label class="control-label">电话:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <div class="input-group">
                                    <input type="text" class="form-control" name="phone[]">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default phone_add" type="button"><span class="glyphicon glyphicon-plus "></span></button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="control-group row tmpRemovePhone" style="display: none">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <div class="input-group">
                                    <input type="text" class="form-control" name="phone[]">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default phone_remove" type="button"><span class="glyphicon glyphicon-minus"></span></button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="control-group row addOfficeTelDiv">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                            <label class="control-label">办公电话:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <div class="input-group">
                                    <input type="text" class="form-control" name="office_tel[]">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default office_tel_add" type="button"><span class="glyphicon glyphicon-plus "></span></button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="control-group row tmpRemoveOfficeTel" style="display: none">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <div class="input-group">
                                    <input type="text" class="form-control" name="office_tel[]">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default office_tel_remove" type="button"><span class="glyphicon glyphicon-minus"></span></button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="control-group row addResidenceTelDiv">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                            <label class="control-label">住宅电话:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <div class="input-group">
                                    <input type="text" class="form-control" name="residence_tel[]">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default residence_tel_add" type="button"><span class="glyphicon glyphicon-plus "></span></button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="control-group row tmpRemoveResidenceTel" style="display: none">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <div class="input-group">
                                    <input type="text" class="form-control" name="residence_tel[]">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default residence_tel_remove" type="button"><span class="glyphicon glyphicon-minus"></span></button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="control-group row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                            <label class="control-label" for="company">单位:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <input type="text" class="form-control" id="company" name="company">
                            </div>
                        </div>
                    </div>

                    <div class="control-group row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                            <label class="control-label" for="position">职务:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <input type="text" class="form-control" id="position" name="position">
                            </div>
                        </div>
                    </div>

                    <div class="control-group row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                            <label class="control-label" for="describe">描述:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <textarea class="form-control" id="describe" name="describe"></textarea>
                            </div>
                        </div>
                    </div>

                    <div class="control-group row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                            <label class="control-label" for="group_id">群组:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <select name="group_id" id="group_id" class="form-control">
                                    <volist name="groupList" id="group">
                                        <option value="{$group.group_id}">{$group.group_name}</option>
                                    </volist>
                                    <!--<option value="1" selected="selected">男</option>-->
                                    <!--<option value="2">女</option>-->
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="control-group row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                            <label class="control-label" >头像:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <div id="icon_div" class="col-sm-5" style="padding-left: 0px;">
                                    <input type="hidden" id="icon" name="icon" value="">

                                    <div class="image_show" style="display: none">

                                    </div>
                                    <div class="image_handle" data-toggle="tooltip" data-placement="top"
                                         title=""
                                         data-original-title="建议上传宽480px高320px的图片">
                                        <div class="dropped"></div>
                                    </div>
                                </div>
                                <div class="col-sm-5" style="margin-top: 10px; clear: left;">
                                    <a href="javascript:void(0)" id="removeImg" class="btn btn-info invisible"
                                       role="button">删除图片</a>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
                <input type="hidden" name="mail_list_id" id="mail_list_id" value="">
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary btn-flat">确定</button>
                    <button type="button" class="btn btn-default btn-flat" data-dismiss="modal">取消</button>
                </div>

            </form>
        </div>
    </div>

    <div class="modal fade" id="confirm_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">确认</h4>
                </div>
                <div class="modal-body">
                    确定要删除吗?
                </div>
                <div class="modal-footer">
                    <button type="button" id="commonDel" class="btn btn-danger btn-flat">删除</button>
                    <button type="button" class="btn btn-default btn-flat" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
<!-- <style type="text/css">
    #tree .list-group{
        display: block;
        height: 700px;
        overflow: auto;
    }
</style> -->
</block>
<block name="script">
    <script src="__STATIC__/scripts/vendors/bootstrap-select.min.js"></script>
    <script src="__STATIC__/scripts/vendors/bootstrap-table.js"></script>
    <script src="__STATIC__/plugins/dropper/jquery.fs.dropper.min.js"></script>
    <script src="__STATIC__/scripts/vendors/bootstrap-treeview.min.js"></script>
    <script src="__STATIC__/scripts/vendors/fileinput.min.js"></script>
    <script src="__STATIC__/scripts/vendors/jquery.form.min.js"></script>
    <script>

        var $table = $('#table');
        var $complexModal = $('#complex_modal');
        var $groupTable = $('#group-table');
        var group_code = 0;
        var delMailList = true;
        var TableInit;
        var $confirmDel = $('#commonDel');
        var $importModal = $('#import_modal');
        var oMailTree;
        var $tree = $('#tree');

        function initPhoneController(){
            $('.tmpRemovePhone').not('.tmpRemovePhone:last').remove();
            $('.tmpRemoveOfficeTel').not('.tmpRemoveOfficeTel:last').remove();
            $('.tmpRemoveResidenceTel').not('.tmpRemoveResidenceTel:last').remove();
        }

        function initComplexModal(data){
            clearImageView();
            initPhoneController();
            for(var p in data){
                if(typeof(data[p]) != "function"){
                    $complexModal.find('#' + p).val(data[p]) ;
                }
            }

            var phone = data.phone;
            var office_tel = data.office_tel;
            var residence_tel = data.residence_tel;
            phone = phone.split(',');
            for(var p in phone){
                var parent = $('.addPhoneDiv');
                if(p > 0){
                    var cloneObj = $('.tmpRemovePhone:last').clone();
                    cloneObj.show();
                    parent.after(cloneObj);
                    cloneObj.find('input[type=text]').val(phone[p]);
                }else{
                    parent.find('input[type=text]').val(phone[p]);
                }

            }

            office_tel = office_tel.split(',');
            for(var p in office_tel){
                var parent = $('.addOfficeTelDiv');
                if(p > 0){
                    var cloneObj = $('.tmpRemoveOfficeTel:last').clone();
                    cloneObj.show();
                    parent.after(cloneObj);
                    cloneObj.find('input[type=text]').val(office_tel[p]);
                }else{
                    parent.find('input[type=text]').val(office_tel[p]);
                }

            }

            residence_tel = residence_tel.split(',');
            for(var p in residence_tel){
                var parent = $('.addResidenceTelDiv');
                if(p > 0){
                    var cloneObj = $('.tmpRemoveResidenceTel:last').clone();
                    cloneObj.show();
                    parent.after(cloneObj);
                    cloneObj.find('input[type=text]').val(residence_tel[p]);
                }else{
                    parent.find('input[type=text]').val(residence_tel[p]);
                }

            }


            if(data.icon){
                viewImageTointro(data.icon);
            }
            $complexModal.find('.modal-title').text('编辑联系人');
        }

        function initDropper(){
            $('#icon_div .dropped').dropper({
                postKey: "file",
                action: "{:U('File/uploadPicture')}",
                postData: {thumbSizes: '320x480', is_maillist:1},
                label: "把文件拖拽到此处",
                maxSize: 1048576
            }).on("fileComplete.dropper", onFileComplete)
                    .on("fileError.dropper", onFileError);

            $('#removeImg').on("click",clearImageView );
        }

        function onFileComplete(e, file, response) {
            if(response.status == 0){
                layer.msg("抱歉上传失败");
            }else{
                viewImageTointro(response.info);
            }
        }

        function onFileError(e, file, error) {
            layer.msg(error);
        }
        function getHeight(){
            return $('#right').height() - 30;
        }
        function viewImageTointro(image) {
            if (image) {
                var root = "{:C('MAILLIST_ICON_URL')}";
                var imagesrc = root + '/' + image;
                $("#icon_div").find(".dropper-input").val("");
                $("#icon_div").find(".image_handle").hide();
                $("#icon_div").find(".image_show").show();
                $("#icon").val(image);
                $("#icon_div").find(".image_show").html("<img id='icon_preview' src='" + imagesrc + "' class='img-responsive' >");
                $("#removeImg").removeClass('invisible');
            }
        }

        function clearImageView () {
            var path_intro = $("#icon_preview").prop("src");
            $("#icon").val("");
            $("#icon_div").find(".image_show").html("");
            $("#icon_div").find(".image_handle").show();
            $("#icon_div").find(".dropper-input").val("");
            $("#removeImg").addClass('invisible');
        }

  //树形列表     
        var MailTree = function(){
            var oMailTree = new Object();

            oMailTree.init = function(data){
                $tree.treeview({
                    data:data,
                    silent:true,
                    showBorder:false,
                    levels:1,
                    onNodeSelected:function (event, data) {
                        group_code = data.group_code;
                        if(TableInit){
                            $table.bootstrapTable('destroy');
                            TableInit.init();
                        }
                    }

                });
            }

            oMailTree.refresh= function(){
                $.ajax({
                    type:'post',
                    url:"{:U('getMail')}",
                    success:function(data){
                        if(data.status == 1){
                            oMailTree.init(data.data);
                        }else{
                            layer.msg(data.msg);
                        }
                    }
                });
            };

            return oMailTree;
        }

// 树形结束


        var TableInit = function(){
            var oTableInit = new Object();

            oTableInit.selections = [];

            oTableInit.groupSelections = [];

            oTableInit.init = function(){
                $table.bootstrapTable({
                    formatLoadingMessage: function () {
                        return '正在努力地加载数据中，请稍候……';
                    },
                    formatRecordsPerPage: function (pageNumber) {
                        return '每页显示 ' + pageNumber + ' 条记录';
                    },
                    formatShowingRows: function (pageFrom, pageTo, totalRows) {
                        return '显示第 ' + pageFrom + ' 到第 ' + pageTo + ' 条记录，总共 ' + totalRows + ' 条记录';
                    },
                    formatSearch: function () {
                        return '姓名搜索';
                    },
                    formatNoMatches: function () {
                        return '没有找到匹配的记录';
                    },
                    formatPaginationSwitch: function () {
                        return '隐藏/显示分页';
                    },
                    formatRefresh: function () {
                        return '刷新';
                    },
                    formatToggle: function () {
                        return '切换';
                    },
                    formatColumns: function () {
                        return '列';
                    },
                    url:"{:U('mailList')}",
                    method:'get',
                    striped:true,
                    cache:false,
                    pagination:true,
                    sortable:false,
                    sortOrder:'asc',
                    sidePagination:'server',
                    queryParams:oTableInit.mailListQueryParams,
                    pageNumber:1,
                    pageSize:10,
                    pageList:[10,20,50],
                    uniqueId:'mail_list_id',
                    search:true,
                    height:getHeight(),

                    columns:[
                        {
                            field:'state',
                            checkbox:true,
                            align:'center'
                        },
                        {
                            field:'mail_list_id',
                            title:'序号',
                            visible:false
                        },
                        {
                            field:'real_name',
                            title:'姓名',
                            width:'60px',
                            formatter:function (value){
                                return '<a href="javascript: void 0" class="mail_list_id">' + value + '</a>';
                            },
                            events:operateEvents
                        },
                        {
                            field:'phone',
                            title:'号码'
                        },
                        {
                            field:'office_tel',
                            title:'办公电话'
                        },
                        {
                            field:'residence_tel',
                            title:'住宅电话'
                        },
                        {
                            field:'position',
                            title:'职务'
                        },
                        {
                            field:'company',
                            title:'单位'
                        },
                        {
                            field:'describe',
                            title:'描述'
                        },
                        {
                            field:'add_time',
                            title:'添加时间'
                        },
                        {
                            field:'group_name',
                            title:'所属群组'
                        }

                    ]
                });

//                $groupTable.bootstrapTable({
//                    formatLoadingMessage: function () {
//                        return '正在努力地加载数据中，请稍候……';
//                    },
//                    formatRecordsPerPage: function (pageNumber) {
//                        return '每页显示 ' + pageNumber + ' 条记录';
//                    },
//                    formatShowingRows: function (pageFrom, pageTo, totalRows) {
//                        return '显示第 ' + pageFrom + ' 到第 ' + pageTo + ' 条记录，总共 ' + totalRows + ' 条记录';
//                    },
//                    formatSearch: function () {
//                        return '搜索';
//                    },
//                    formatNoMatches: function () {
//                        return '没有找到匹配的记录';
//                    },
//                    formatPaginationSwitch: function () {
//                        return '隐藏/显示分页';
//                    },
//                    formatRefresh: function () {
//                        return '刷新';
//                    },
//                    formatToggle: function () {
//                        return '切换';
//                    },
//                    formatColumns: function () {
//                        return '列';
//                    },
//                    height:'80px',
//                    columns:[
//                        {
//                            field:'state',
//                            checkbox:true,
//                            align:'center'
//                        },
//                        {
//                            field:'group_name',
//                            title:'群组名',
//                            align:'center',
//                            formatter:function(value){
//                                return '<a href="javascript: void 0" class="group_name">' + value + '</a>';
//                            },
//                            events:operateEvents
//                        },
//                        {
//                            field:'num',
//                            title:'成员数',
//                            editable:false,
//                            align:'center'
//                        }
//
//                    ],
//                    url:"{:U('groupList')}",
//                    method:'get',
//                    striped:true,
//                    cache:false,
//                    pagination:true,
//                    sortable:false,
//                    sortOrder:'asc',
//                    queryParams:oTableInit.queryParams,
//                    sidePagination:'server',
//                    pageNumber:1,
//                    pageSize:3,
//                    uniqueId:'group_id',
//                    responseHandler:function(res){
//                        return res;
//                    }
//                });


                $(window).resize(function () {
                    $table.bootstrapTable('resetView', {height:getHeight()});
                });

                $table.on('check.bs.table uncheck.bs.table ' +
                    'check-all.bs.table uncheck-all.bs.table', function () {
                    $('#remove').prop('disabled', !$table.bootstrapTable('getSelections').length);

                    // save your data, here just save the current page
                    oTableInit.selections = oTableInit.getIdSelections();
                    // push or splice the selections if you want to save all data selections
                });

//                $groupTable.on('check.bs.table uncheck.bs.table ' +
//                    'check-all.bs.table uncheck-all.bs.table', function () {
//                    $('#deleteGroup').prop('disabled', !$groupTable.bootstrapTable('getSelections').length);
//
//                    // save your data, here just save the current page
//                    oTableInit.groupSelections = oTableInit.getGroupIdSelections();
//                    // push or splice the selections if you want to save all data selections
//                });




            };

            oTableInit.mailListQueryParams = function(params){
                var temp = {
                    limit:params.limit,
                    offset:params.offset,
//                    maxrows:params.limit,
                    search:params.search,
                    group_code:group_code,
                };
                return temp;
            };

            oTableInit.queryParams = function(params){
                var temp = {
                    limit:params.limit,
                    offset:params.offset,
                    maxrows:params.limit
                };
                return temp;
            };

            oTableInit.getIdSelections = function(){
                return $.map($table.bootstrapTable('getSelections'), function (row) {
                    return row.mail_list_id;
                })
            };

            oTableInit.getGroupIdSelections = function(){
                return $.map($groupTable.bootstrapTable('getSelections'),function(row){
                    return row.group_id;
                })
            };

            oTableInit.refresh = function(){
                $table.bootstrapTable('refresh');
            };
            return oTableInit;
        };


        function initValidate(){
            $('#editContact').validate({
                debug:true,
                submitHandler:function (form) {
                    var url = $(form).prop('action');
                    var postData = $(form).serialize();
                    $.post(url,postData, function(data){
                        if(data.status == 1){
                            layer.msg('编辑成功');
                            form.reset();
                            clearImageView();
                            $complexModal.modal('hide');
                            $table.bootstrapTable('refresh');
                        }else{
                            layer.msg('编辑失败');
                        }
                    });
                },
                rules:{
                    real_name:"required",
                    phone:'required'
                },
                messages:{
                    real_name:'请填写姓名',
                    phone:'请填写电话'
                }
            });

            $('#importForm').validate({
                debug:true,
                submitHandler:function (form) {
                    var url = $(form).prop('action');

                    $(form).ajaxSubmit({
                        url:url,
                        success:function(data){
                            if(data.status == 1){
                                var info = '<p>导入结果：</p>';
                                info = info + '<p>成功导入' + data.data.success + '条</p>';
                                info = info + '<p>失败' + data.data.failed + '条</p>';
                                if(data.data.errLines && data.data.errLines.length>0){
                                    for (var i = 0; i < data.data.errLines.length; i++){
                                        info = info + '<p>第' + data.data.errLines[i] + '条导入失败</p>';
                                    }
                                }
                                layer.alert(info, {
                                    skin: 'layui-layer-lan' //样式类名
                                    ,closeBtn: 0,
                                    anim: 1
                                });
                                $importModal.modal('hide');
                                $table.bootstrapTable('refresh');

                            }else{
                                layer.msg(data.msg);
                            }
                        }
                    })

                },
                rules:{
                    file_import:'required'
                },
                messages:{
                    file_import:'请选择上传文件'
                }
            });
        }

        window.operateEvents = {
            'click .mail_list_id':function (e, value, row, index) {
                $complexModal.modal('show');
                initComplexModal(row);
            },
            'click .group_name':function(e, value, row, index){
                var html = '\
    <div class="alert alert-info alert-dismissible fade in" role="alert">\
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>\
      <div class="row">\
        <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7">\
          <div class="controls">\
          <input type="hidden" id="group_id" value="'+row.group_id+'">\
            <input type="text" id="group_name" class="form-control" value="'+row.group_name+'">\
          </div>\
        </div>\
        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">\
          <button type="button" id="editGroup" class="btn btn-primary btn-flat">确定</button>\
          <button type="button" class="btn btn-default btn-flat" data-dismiss="alert" aria-label="Close">取消</button>\
        </div>\
      </div>\
    </div>\
    ';
                $('#table2-alert').html(html);
                $('#editGroup').click(function(){
                    var group_name = $("#group_name").val();
                    var group_id = $("#group_id").val();
                    if(group_name == ''){
                        layer.msg('群组名不能为空');
                    }
                    $.get(
                        "{:U('editGroup')}",
                        {
                            group_id:group_id,
                            group_name:group_name
                        },
                        function(data){
                            if(data.status == 1){
                                layer.msg('修改成功');
                                $groupTable.bootstrapTable('refresh');
                                $('#table2-alert').empty();
                            }else{
                                layer.msg(data.msg);
                            }
                        }
                    );
                });
            }
        };

        $(function () {
            oMailTree = new MailTree();
            oMailTree.refresh();
            initDropper();
            initValidate();
            TableInit = new TableInit();
            TableInit.init();

            $('#tree').css({
                "height":getHeight(),
                "overflow":"auto",
            });
            $('#remove').click(function () {
                delMailList = true;
                $("#confirm_modal").modal('show');
            });

            $('#deleteGroup').click(function () {
                console.log(11111);
                delMailList = false;
                $("#confirm_modal").modal('show');
            });

            $('#group-manage').click(function(){
                $('#group_modal').modal('show');
            });

            $('#add').click(function(){
                $('#editContact')[0].reset();
                clearImageView();
                initPhoneController();
                $('#mail_list_id').val('');
                $complexModal.find('.modal-title').text('添加联系人');
                $complexModal.modal('show');
            });


            $confirmDel.click(function () {
                $("#confirm_modal").modal('hide');
                if(delMailList){
                    $.post({
                        url:"{:U('delete')}",
                        data:{
                            selectedIds : oTable.selections
                        },
                        success:function(data){
                            if(data.status == 1){
                                layer.msg('删除成功');
                                $table.bootstrapTable('refresh');
                                $groupTable.bootstrapTable('refresh');
                            }else{
                                layer.msg('删除失败');
                            }
                        }
                    })
                }else{
                    $.post({
                        url:"{:U('deleteGroup')}",
                        data:{
                            selectedIds : oTable.groupSelections,
                            delOption:$('#delOption').val()
                        },
                        success:function(data){
                            if(data.status == 1){
                                layer.msg('删除成功');
                                $table.bootstrapTable('refresh');
                                $groupTable.bootstrapTable('refresh');
                            }else{
                                layer.msg('删除失败');
                            }
                        }
                    })
                }

            })

            $('#add-group').click(function () {

                var html = '\
                <div class="alert alert-info alert-dismissible fade in" role="alert">\
                  <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>\
                  <div class="row">\
                    <div class="col-lg-7 col-md-7 col-sm-7 col-xs-7">\
                      <div class="controls">\
                        <input type="text" id="group_name" class="form-control" value="">\
                      </div>\
                    </div>\
                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">\
                      <button type="button" id="editGroup" class="btn btn-primary btn-flat">确定</button>\
                      <button type="button" class="btn btn-default btn-flat" data-dismiss="alert" aria-label="Close">取消</button>\
                    </div>\
                  </div>\
                </div>\
                ';
                $('#table2-alert').html(html);

                $('#editGroup').click(function(){
                    var group_name = $("#group_name").val();
//                    var group_id = $("#group_id").val();
                    if(group_name == ''){
                        layer.msg('群组名不能为空');
                    }
                    $.get(
                        "{:U('addGroup')}",
                        {
                            group_name:group_name
                        },
                        function(data){
                            if(data.status == 1){
                                layer.msg('新增成功');
                                $groupTable.bootstrapTable('refresh');
                                $('#table2-alert').empty();
                            }else{
                                layer.msg(data.msg);
                            }
                        }
                    );
                });

            });

            $('.phone_add').click(function () {
                var parent = $(this).closest('.control-group');
                var cloneObj = $('.tmpRemovePhone:last').clone();
                cloneObj.show();
                parent.after(cloneObj);
            });

            $(document).on('click','.phone_remove',function () {
                var parent = $(this).closest('.control-group');
                parent.remove();
            });

            $('.office_tel_add').click(function () {
                var parent = $(this).closest('.control-group');
                var cloneObj = $('.tmpRemoveOfficeTel:last').clone();
                cloneObj.show();
                parent.after(cloneObj);
            });

            $(document).on('click','.office_tel_remove',function () {
                var parent = $(this).closest('.control-group');
                parent.remove();
            });

            $('.residence_tel_add').click(function () {
                var parent = $(this).closest('.control-group');
                var cloneObj = $('.tmpRemoveResidenceTel:last').clone();
                cloneObj.show();
                parent.after(cloneObj);
            });

            $(document).on('click','.residence_tel_remove',function () {
                var parent = $(this).closest('.control-group');
                parent.remove();
            });

            $('#import').click(function () {
               $importModal.modal('show');
            });
            $('#modelLoad').click(function(){
                var root = "{:C('MODEL_LOAD_DOC_MESG')}";
                window.location.href = root;
            });
        });


    </script>
</block>